﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DTO;
using BUS;
namespace GUI
{
    public partial class fmMain : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public fmMain()
        {
            InitializeComponent();
        }
        // Khang 
        NguoiDungDTO nd = null;
        public void setNguoiDung(NguoiDungDTO ndung)
        {
            this.nd = ndung;
        }
        //End Khang 
        private void SetVisibleBarDocument(String textName)
        {
            dnbmanagerMain.SuspendLayout = true; // don't understand why need it set suspendlayout = true // hoan cach trinh bay
            try
            {
                foreach (Bar b in dnbmanagerMain.Bars)
                {
                    if (b.DockSide == eDockSide.Document) // type of it is document
                    {
                        foreach (DockContainerItem dci in b.Items)
                        {
                            if (dci.Text.Equals(textName))
                            {
                                dci.Focus();
                                dci.Visible = true;
                                if (!b.Visible)
                                    b.Visible = true;
                                b.SelectedDockTab = b.Items.IndexOf(dci);

                            }
                        }
                    }

                }
            }
            finally
            {
                dnbmanagerMain.SuspendLayout = false;
            }
        }

        #region sách
        
        private void btnQLSach_Click(object sender, EventArgs e)
        {
            // show dci when it closed
            SetVisibleBarDocument("Sách");
            /*Dim sc As New Sanh_Cuoi()
            'show dci len khi da dong 
            listSanh = sc.LayDanhSach(iSCurPage, iPerPage)
            iSPage = sc.LaySoDong()
            LoadSanh(iSPage)*/

        }

        private void lvSach_ItemActivate(object sender, EventArgs e)
        {//this event do when you double click into lv or enter into listview item exists values
            MessageBox.Show("active");
        }
        #endregion

        #region create memory for listview, dockcontaineritem, bar final dock them to docksiteContainer
        private Bar GetBarContainer(){
            foreach (Bar b in dnbmanagerMain.Bars){
                if (b.DockSide == eDockSide.Document && b.Name == "barContainer"){
                    return b;
                }
	        }
            //If no documents bars found, create new one 
            DevComponents.DotNetBar.Bar bar  = BarUtilities.CreateDocumentBar();
            bar.Name = "barContainer";
            dockSiteContainer.GetDocumentUIManager().Dock(bar);
            return bar;
        }
        private void CreateDockContainerItemListview( ref DevComponents.DotNetBar.DockContainerItem dci,ref DevComponents.DotNetBar.PanelDockContainer pdc,ref ListView lv){
            //create memory of dci called and add it to exsting bar
            dci = new DevComponents.DotNetBar.DockContainerItem();
            //create memory panel to host any control(contain datagridview)
            pdc = new DevComponents.DotNetBar.PanelDockContainer();
            //create memory of dataListview to show any data
            lv = new ListView();
            // add the listview to pdc 
            pdc.Controls.Add(lv);
            //set adtribute to lv
            lv.Dock = DockStyle.Fill;
            lv.FullRowSelect = true;
            lv.GridLines = true;
            lv.View = View.Details;
            //
            dci.Control = pdc;
            // get a bar from form main
            DevComponents.DotNetBar.Bar bar = GetBarContainer();
            bar.Items.Add(dci);
            if (!bar.Visible)
                bar.Visible = true;
            //select dci created new to it show over dci collection
            bar.SelectedDockTab = bar.Items.IndexOf(dci);
            bar.RecalcLayout(); //recall layout all dci in barcontainer

        }
        #endregion
        
        #region quản lý khách hàng
        private DevComponents.DotNetBar.DockContainerItem dciDSKH;
        private List<KhachHangDTO> listKhachHang;
        //Private ListTiecCuoi As IList 'List(Of TiecCuoiDTO)
        // vb'show trên form chính trog document <nhưng mà khi có rùi thì click lại thì chỉ cho forcus vào mà thôi> => dim ở ngoài cùng
        private ListView lvDSKH;
        private DevComponents.DotNetBar.PanelDockContainer pdcDSKH;
        private void InitListViewDSKH(ListView lv){
            ColumnHeader clStt = new ColumnHeader();
            ColumnHeader clMaKH = new ColumnHeader();
            ColumnHeader clTenKH = new ColumnHeader();
            ColumnHeader clDiaChi = new ColumnHeader();
            ColumnHeader clDienThoai = new ColumnHeader();
            ColumnHeader clSoTienNo = new ColumnHeader();
            clStt.Text = "Stt";
            clStt.Width =35;
            clMaKH.Text = "Mã Khách Hàng";
            clMaKH.Width =150;
            clTenKH.Text = "Tên Khách Hàng";
            clTenKH.Width =250;
            clDiaChi.Text = "Địa Chỉ";
            clDiaChi.Width =300;
            clDienThoai.Text = "Điện Thoại";
            clDienThoai.Width =135;
            clSoTienNo.Text = "Số Tiền Nợ";
            clSoTienNo.Width =135;
            lvDSKH.Columns.AddRange(new ColumnHeader[]{clStt, clMaKH, clTenKH,clDiaChi,clDienThoai,clSoTienNo});
        }

        private void lvDSKH_ItemActivate(object sender, EventArgs e)
        {//this event do when you double click into lv or enter into listview item exists values
            MessageBox.Show("active");
        }

        private void btnQLKhachHang_Click(object sender, EventArgs e)
        {
            if (dciDSKH == null)
            {
                CreateDockContainerItemListview(ref dciDSKH, ref pdcDSKH, ref lvDSKH);
                dciDSKH.Text = "Khách Hàng";
                //'create proprety of listview nhân viên
                InitListViewDSKH(lvDSKH);
                this.lvDSKH.ItemActivate += new System.EventHandler(this.lvDSKH_ItemActivate);
            }
            SetVisibleBarDocument("Khách Hàng");
            
            /*// must check when turn off and next click open, this event loads data, so you must handle to event show but none load
            //busKhachHang. // 
        Dim tc As New Tiec_Cuoi()
        iTCCurPage = 0
        ListTiecCuoi = tc.LayDanhSachTiec(iTCCurPage, iPerPage)
        iTCPage = tc.LaySoDong()
        lbTCPage.Text = (iTCCurPage + 1).ToString() + "/" + ((iTCPage \ iPerPage) + 1).ToString()
        RefeshListViewTiecCuoi(lvTiecCuoi, ListTiecCuoi)
             */
        }
#endregion

        #region // quản lý hoá đơn nhập
        private DevComponents.DotNetBar.DockContainerItem dciDSNhap;
        private List<KhachHangDTO> listNhap;
        //Private ListTiecCuoi As IList 'List(Of TiecCuoiDTO)
        // vb'show trên form chính trog document <nhưng mà khi có rùi thì click lại thì chỉ cho forcus vào mà thôi> => dim ở ngoài cùng
        private ListView lvDSNhap;
        private DevComponents.DotNetBar.PanelDockContainer pdcDSNhap;
        private void InitListViewDSNhap(ListView lv)
        {
            ColumnHeader clStt = new ColumnHeader();
            ColumnHeader clNgayNhap = new ColumnHeader();
            ColumnHeader clNhaCungCap = new ColumnHeader();
            ColumnHeader clDonGiaTong = new ColumnHeader();
            clStt.Text = "Stt";
            clStt.Width = 35;
            clNgayNhap.Text = "Ngày Nhập";
            clNgayNhap.Width = 150;
            clNhaCungCap.Text = "Nhà Cung Cấp";
            clNhaCungCap.Width = 350;
            clDonGiaTong.Text = "Tổng Tiền Hoá Đơn";
            clDonGiaTong.Width = 250;
            lvDSNhap.Columns.AddRange(new ColumnHeader[] { clStt,clNgayNhap, clNhaCungCap, clDonGiaTong });
        }
         private void lvDSNhap_ItemActivate(object sender, EventArgs e)
        {//this event do when you double click into lv or enter into listview item exists values
            MessageBox.Show("active");
        }
        private void btnQLHoaDonNhap_Click(object sender, EventArgs e)
        {
            if (dciDSNhap == null)
            {
                CreateDockContainerItemListview(ref dciDSNhap, ref pdcDSNhap, ref lvDSNhap);
                dciDSNhap.Text = "Hoá Đơn Nhập";
                //'create proprety of listview nhân viên
                InitListViewDSNhap(lvDSNhap);
                this.lvDSNhap.ItemActivate += new System.EventHandler(this.lvDSNhap_ItemActivate);
            }
            SetVisibleBarDocument("Hoá Đơn Nhập");
            /*// must check when turn off and next click open, this event loads data, so you must handle to event show but none load
            //busKhachHang. /*/ 
        }
#endregion

        #region // quản lý hoá đơn Bán
        private DevComponents.DotNetBar.DockContainerItem dciDSBan;
        private List<KhachHangDTO> listBan;
        //Private ListTiecCuoi As IList 'List(Of TiecCuoiDTO)
        // vb'show trên form chính trog document <nhưng mà khi có rùi thì click lại thì chỉ cho forcus vào mà thôi> => dim ở ngoài cùng
        private ListView lvDSBan;
        private DevComponents.DotNetBar.PanelDockContainer pdcDSBan;
        private void InitListViewDSBan(ListView lv)
        {
            ColumnHeader clStt = new ColumnHeader();
            ColumnHeader clNgayBan = new ColumnHeader();
            ColumnHeader clMaKhachHang = new ColumnHeader();
            ColumnHeader clTenKhachHang = new ColumnHeader();
            ColumnHeader clSoLuongSach = new ColumnHeader();
            ColumnHeader clTongTien = new ColumnHeader();
            ColumnHeader clTienKhachTra = new ColumnHeader();
            clStt.Text = "Stt";
            clStt.Width = 35;
            clNgayBan.Text = "Ngày Bán";
            clNgayBan.Width = 120;
            clMaKhachHang.Text = "Mã Khách Hàng";
            clMaKhachHang.Width = 150;
            clTenKhachHang.Text = "Tên Khách Hàng";
            clTenKhachHang.Width = 220;
            clSoLuongSach.Text = "Số Lượng Sách Bán";
            clSoLuongSach.Width = 120;
            clTongTien.Text = "Tổng Tiền";
            clTongTien.Width = 200;
            clTienKhachTra.Text = "Tổng Tiền Thu KHách Hàng";
            clTienKhachTra.Width = 200;
            lvDSBan.Columns.AddRange(new ColumnHeader[] { clStt, clNgayBan, clMaKhachHang, clTenKhachHang, clSoLuongSach, clTongTien, clTienKhachTra });
        }
        private void lvDSBan_ItemActivate(object sender, EventArgs e)
        {//this event do when you double click into lv or enter into listview item exists values
            MessageBox.Show("active");
        }
        private void btnQLHoaDonBan_Click(object sender, EventArgs e)
        {
            if (dciDSBan == null)
            {
                CreateDockContainerItemListview(ref dciDSBan, ref pdcDSBan, ref lvDSBan);
                dciDSBan.Text = "Hoá Đơn Bán";
                //'create proprety of listview nhân viên
                InitListViewDSBan(lvDSBan);
                this.lvDSBan.ItemActivate += new System.EventHandler(this.lvDSBan_ItemActivate);
            }
            SetVisibleBarDocument("Hoá Đơn Bán");

            /*// must check when turn off and next click open, this event loads data, so you must handle to event show but none load
            //busKhachHang. /*/
        }
        #endregion

        #region // quản lý hoá đơn thu nợ
        private DevComponents.DotNetBar.DockContainerItem dciDSThuNo;
        private List<KhachHangDTO> listThuNo;
        //Private ListTiecCuoi As IList 'List(Of TiecCuoiDTO)
        // vb'show trên form chính trog document <nhưng mà khi có rùi thì click lại thì chỉ cho forcus vào mà thôi> => dim ở ngoài cùng
        private ListView lvDSThuNo;
        private DevComponents.DotNetBar.PanelDockContainer pdcDSThuNo;
        private void InitListViewDSThuNo(ListView lv)
        {
            ColumnHeader clStt = new ColumnHeader();
            ColumnHeader clNgayThuNo = new ColumnHeader();
            ColumnHeader clMaKhachHang = new ColumnHeader();
            ColumnHeader clTenKhachHang = new ColumnHeader();
            ColumnHeader clSoTienThu = new ColumnHeader();
            clStt.Text = "Stt";
            clStt.Width = 35;
            clNgayThuNo.Text = "Ngày Thu";
            clNgayThuNo.Width = 150;
            clMaKhachHang.Text = "Mã Khách Hàng";
            clMaKhachHang.Width = 150;
            clTenKhachHang.Text = "Tên Khách Hàng";
            clTenKhachHang.Width = 300;
            clSoTienThu.Text = "Số Tiền Thu";
            clSoTienThu.Width = 150;
            lvDSThuNo.Columns.AddRange(new ColumnHeader[] { clStt, clNgayThuNo, clMaKhachHang, clTenKhachHang,clSoTienThu });
        }
        private void lvDSThuNo_ItemActivate(object sender, EventArgs e)
        {//this event do when you double click into lv or enter into listview item exists values
            MessageBox.Show("active");
        }
        private void btnQLHoaDonThuNo_Click(object sender, EventArgs e)
        {
            if (dciDSThuNo == null)
            {
                CreateDockContainerItemListview(ref dciDSThuNo, ref pdcDSThuNo, ref lvDSThuNo);
                dciDSThuNo.Text = "Hoá Đơn Thu Nợ Khách Hàng";
                //'create proprety of listview nhân viên
                InitListViewDSThuNo(lvDSThuNo);
                this.lvDSThuNo.ItemActivate += new System.EventHandler(this.lvDSThuNo_ItemActivate);
            }
            SetVisibleBarDocument("Hoá Đơn Thu Nợ Khách Hàng");

            /*// must check when turn off and next click open, this event loads data, so you must handle to event show but none load
            //busKhachHang. /*/
        }
        #endregion

        #region quản lý khách hàng đang nợ tiền
        /*this is also list khach hang, but it flite khach hang having sotienno > 0 */
        private DevComponents.DotNetBar.DockContainerItem dciDSKHNo;
        private List<KhachHangDTO> listKhachHangNo;
        //Private ListTiecCuoi As IList 'List(Of TiecCuoiDTO)
        // vb'show trên form chính trog document <nhưng mà khi có rùi thì click lại thì chỉ cho forcus vào mà thôi> => dim ở ngoài cùng
        private ListView lvDSKHNo;
        private DevComponents.DotNetBar.PanelDockContainer pdcDSKHNo;
        private void InitListViewDSKHNo(ListView lv)
        {
            ColumnHeader clStt = new ColumnHeader();
            ColumnHeader clMaKH = new ColumnHeader();
            ColumnHeader clTenKH = new ColumnHeader();
            ColumnHeader clDiaChi = new ColumnHeader();
            ColumnHeader clDienThoai = new ColumnHeader();
            ColumnHeader clSoTienNo = new ColumnHeader();
            clStt.Text = "Stt";
            clStt.Width = 35;
            clMaKH.Text = "Mã Khách Hàng";
            clMaKH.Width = 150;
            clTenKH.Text = "Tên Khách Hàng";
            clTenKH.Width = 250;
            clDiaChi.Text = "Địa Chỉ";
            clDiaChi.Width = 300;
            clDienThoai.Text = "Điện Thoại";
            clDienThoai.Width = 135;
            clSoTienNo.Text = "Số Tiền Nợ";
            clSoTienNo.Width = 135;
            lvDSKHNo.Columns.AddRange(new ColumnHeader[] { clStt, clMaKH, clTenKH, clDiaChi, clDienThoai, clSoTienNo });
        }

        private void lvDSKHNo_ItemActivate(object sender, EventArgs e)
        {//this event do when you double click into lv or enter into listview item exists values
            MessageBox.Show("active");
        }
        
        private void btnQLKhachHangNo_Click(object sender, EventArgs e)
        {
            if (dciDSKHNo == null)
            {
                CreateDockContainerItemListview(ref dciDSKHNo, ref pdcDSKHNo, ref lvDSKHNo);
                dciDSKHNo.Text = "Khách Hàng Đang Nợ Tiền";
                //'create proprety of listview nhân viên
                InitListViewDSKHNo(lvDSKHNo);
                this.lvDSKHNo.ItemActivate += new System.EventHandler(this.lvDSKHNo_ItemActivate);
            }
            SetVisibleBarDocument("Khách Hàng Đang Nợ Tiền");

            /*// must check when turn off and next click open, this event loads data, so you must handle to event show but none load
            //busKhachHang. // 
            Dim tc As New Tiec_Cuoi()
            iTCCurPage = 0
            ListTiecCuoi = tc.LayDanhSachTiec(iTCCurPage, iPerPage)
            iTCPage = tc.LaySoDong()
            lbTCPage.Text = (iTCCurPage + 1).ToString() + "/" + ((iTCPage \ iPerPage) + 1).ToString()
            RefeshListViewTiecCuoi(lvTiecCuoi, ListTiecCuoi)
             */
        }
        #endregion

        private void btnHTDangNhap_Click(object sender, EventArgs e)
        {
            /*Dim fDangNhap As frmDangNhap = New frmDangNhap()
        ' vì ta lấy từ form về nên phải sau khi form load thì mới có form.tag
        fDangNhap.ShowDialog()
        userLogin = fDangNhap.Tag
        If userLogin Is Nothing Then
            'userLogin = New USERNAME() ' new lai vi dang nhap ma ko vao dc -> view sach error
            Exit Sub
        End If
        RefeshLogin()*/
            frmDangNhap frmDangNhap = new frmDangNhap();
            frmDangNhap.ShowDialog();
        }

        frmLoaiSach _frmLoai;// = new frmLoaiSach();
        private void btnQLLoaiSach_Click(object sender, EventArgs e)
        {
            if (_frmLoai == null || _frmLoai.Created == false) 
            {
                _frmLoai = new frmLoaiSach();
            }
            _frmLoai.Focus();
            _frmLoai.WindowState = FormWindowState.Normal;
            _frmLoai.setNguoiDung(nd);
            _frmLoai.Show();
            /*If 
            ' coi có mở form chưa
            If ((fTCCa Is Nothing) OrElse (fTCCa.Created = False)) Then
                fTCCa = New frmTCCa()
            End If
            */  
        }
        frmNhapSach frmNhapSach;
        private void btnQLNhapHang_Click(object sender, EventArgs e)
        {
            if (frmNhapSach == null || frmNhapSach.Created == false)
            {
                frmNhapSach = new frmNhapSach();
            }
            frmNhapSach.Focus();
            frmNhapSach.WindowState = FormWindowState.Normal;
            frmNhapSach.Show();

        }
        frmThanhToan frmThanhToan;
        private void btnQLThanhToan_Click(object sender, EventArgs e)
        {
            if (frmThanhToan == null || frmThanhToan.Created == false)
            {
                frmThanhToan = new frmThanhToan();
            }
            frmThanhToan.Focus();
            frmThanhToan.WindowState = FormWindowState.Normal;
            frmThanhToan.Show();
        }
        frmDoiMatKhau frmDoiMatKhau;
        private void btnHTDoiMatKhau_Click(object sender, EventArgs e)
        {
            if (frmDoiMatKhau == null || frmDoiMatKhau.Created == false)
            {
                frmDoiMatKhau = new frmDoiMatKhau();
            }
            frmDoiMatKhau.setNguoiDung(nd);
            frmDoiMatKhau.Focus();
            frmDoiMatKhau.WindowState = FormWindowState.Normal;
            frmDoiMatKhau.Show();
        }
        frmThongTin frmThongTin;
        private void btnTGThongTin_Click(object sender, EventArgs e)
        {
            if (frmThongTin == null || frmThongTin.Created == false)
            {
                frmThongTin = new frmThongTin();
            }
            frmThongTin.Focus();
            frmThongTin.WindowState = FormWindowState.Normal;
            frmThongTin.Show();
        }

        frmThuTienNo frmThuTien;
        private void btnQLThuTien_Click(object sender, EventArgs e)
        {
            if (frmThuTien == null || frmThuTien.Created == false)
            {
                frmThuTien = new frmThuTienNo();
            }
            frmThuTien.Focus();
            frmThuTien.WindowState = FormWindowState.Normal;
            frmThuTien.Show();
        }

        private void btnTGHuongDan_Click(object sender, EventArgs e)
        {
            busSystemConfig openFile = new busSystemConfig();
            try
            {
                openFile.OpenFile(Application.StartupPath);
            }
            catch (Exception)
            {
                MessageBox.Show("Can't find file help in folder setup", "BookStore");
            }
            
        }

        frmNguoiDung frmNguoiDung;
        private void btnNguoiDung_Click(object sender, EventArgs e)
        {
            // phải phân quyền admin mới vào được form này 
            //admin chỉ khác với người dùng thường ở form này mà thôi
            if (frmNguoiDung == null || frmNguoiDung.Created == false)
            {
                frmNguoiDung = new frmNguoiDung();
            }
            frmNguoiDung.Focus();
            frmNguoiDung.WindowState = FormWindowState.Normal;
            frmNguoiDung.StartPosition = FormStartPosition.CenterScreen;
            frmNguoiDung.Show();
        }
        

    }
}
